package com.flipdf.viewer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;

import com.flipdf.viewer.excel.ExcelDialog;
import com.flipdf.viewer.excel.Excel.Row;
import com.flipdf.viewer.print.PDFPublisher;
import com.flipdf.viewer.stamp.Stamp;
import com.flipdf.viewer.stamp.TextStamp;
import com.flipdf.viewer.stamp.VariableData;

public class PageScrollPanel extends JPanel implements ItemListener {

	private JFrame parent;
	private PagePanel page;

	private List<Row> rows;

	private Rule columnView;
	private Rule rowView;
	private JToggleButton isMetric;
	private JScrollPane pageScrollPane;

	public PageScrollPanel(PagePanel page, JFrame parent) {
		this.parent = parent;
		this.page = page;

		this.setBackground(Color.DARK_GRAY);
		setLayout(new BorderLayout());

		createPagePanel();
		createToolbar();
	}

	private void createPagePanel() {
		columnView = new Rule(Rule.HORIZONTAL, true);
		rowView = new Rule(Rule.VERTICAL, true);

		if (page != null) {
			columnView.setPreferredWidth((int) page.getPreferredSize().getWidth());
			rowView.setPreferredHeight((int) page.getPreferredSize().getHeight());
		} else {
			columnView.setPreferredWidth(320);
			rowView.setPreferredHeight(480);
		}

		JPanel buttonCorner = new JPanel(); // use FlowLayout
		isMetric = new JToggleButton("cm", true);
		isMetric.setFont(new Font("SansSerif", Font.PLAIN, 11));
		isMetric.setMargin(new Insets(2, 2, 2, 2));
		isMetric.addItemListener(this);
		buttonCorner.add(isMetric);

		pageScrollPane = new JScrollPane();
		pageScrollPane.setBackground(Color.DARK_GRAY);
		pageScrollPane.setViewportView(page);

		pageScrollPane.setColumnHeaderView(columnView);
		pageScrollPane.setRowHeaderView(rowView);
		pageScrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, buttonCorner);
		pageScrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, new Corner());
		pageScrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new Corner());

		add(pageScrollPane, BorderLayout.CENTER);
		pageScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black));
	}

	private void createToolbar() {
		JPanel toolbar = new JPanel();
		final JComboBox zoomComboBox = new JComboBox();
		zoomComboBox.addItem(new ZoomItem(1.0F));
		zoomComboBox.addItem(new ZoomItem(2.0F));
		zoomComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (zoomComboBox.getSelectedItem() != null) {
					ZoomItem item = (ZoomItem) zoomComboBox.getSelectedItem();
					page.zoom(item.getRatio());
					pageScrollPane.revalidate();
				}
			}
		});

		toolbar.add(zoomComboBox);
		this.add(toolbar, BorderLayout.NORTH);
		createStampTool(toolbar);
		createPrintTool(toolbar);
	}

	private void createStampTool(JPanel toolbar) {
		JButton textButton = new JButton("T");
		toolbar.add(textButton);
		textButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ExcelDialog dlg = new ExcelDialog(parent);
				dlg.setVisible(true);
				if (dlg.isOk()) {
					String column = dlg.getColumn();
					rows = dlg.getRows();
					Stamp stamp = new TextStamp(column);
					page.addStamp(stamp);
				}
			}
		});
	}

	private void createPrintTool(JPanel toolbar) {
		JButton printButton = new JButton("P");
		toolbar.add(printButton);
		printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				List<String> header = rows.get(0).getRowData();

				List<VariableData> datas = new ArrayList<VariableData>();
				for (int i = 1; i < rows.size(); i++) {
					List<String> row = rows.get(i).getRowData();
					VariableData vd = new VariableData();
					for (int j = 0; j < header.size(); j++) {
						vd.addEntry(header.get(j), row.get(j));
					}
					datas.add(vd);
				}
				PDFPublisher publisher = new PDFPublisher(page.getOwner(), page.getStamps(), datas);
				try {
					publisher.publish();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				javax.swing.JOptionPane.showMessageDialog(parent, "Publish finished!");
			}
		});
	}

	public void itemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			// Turn it to metric.
			rowView.setIsMetric(true);
			columnView.setIsMetric(true);
		} else {
			// Turn it to inches.
			rowView.setIsMetric(false);
			columnView.setIsMetric(false);
		}
		page.setMaxUnitIncrement(rowView.getIncrement());
	}

	class ZoomItem {
		private float ratio = 1.0F;
		public static final String PERCENT = "%";

		public ZoomItem(float ratio) {
			this.ratio = ratio;
		}

		public String toString() {
			return (int) (ratio * 100) + PERCENT;
		}

		public float getRatio() {
			return ratio;
		}

		public boolean equal(Object obj) {
			if (!(obj instanceof ZoomItem))
				return false;
			return (((ZoomItem) obj).getRatio() == ratio);
		}

	}
}
